package riteh.proba.server.resources;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Time {
	public static String AddTime(String original)
	{

		Calendar datum = Calendar.getInstance();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(dateFormat.format(datum.getTime()));
		
		//2008-04-16 00:05:05 calendar class
		
		//Calendar c = Calendar.getInstance(); //sadašnji datum i vrijeme u milisekundama
		
		
		//String original = "12h 15min";
		//String original = "15min";
		original = original.replaceAll(" ", "");
		int i;
		
		String strHours="";
		String strMinutes="";
		int indHours = 0;
		int indMinutes = 0;
		
		indHours =  original.indexOf("h");	
		indMinutes = original.indexOf("min");
		
		if((indHours!=-1)&&(indMinutes!=-1)){
			for(i=0;i<indHours;i++ ){
				strHours+=original.charAt(i);
			}
			for(i=indHours+1;i<indMinutes;i++ ){
				strMinutes+=original.charAt(i);
			}

			datum.setTimeInMillis(datum.getTimeInMillis()+60000*Integer.parseInt(strMinutes)+3600000*Integer.parseInt(strHours));
		}else{
			if ((indMinutes!=-1)&&(indHours==-1)){
				for(i=0;i<indMinutes;i++ ){
					strMinutes+=original.charAt(i);
					
					//System.out.print(original.charAt(i));
				}
				datum.setTimeInMillis(datum.getTimeInMillis()+60000*Integer.parseInt(strMinutes));
			}
			else if ((indMinutes==-1)&&(indHours!=-1)){
				for(i=0;i<indHours;i++ ){
					strHours+=original.charAt(i);
					
					//System.out.print(original.charAt(i));
				}
				datum.setTimeInMillis(datum.getTimeInMillis()+3600000*Integer.parseInt(strHours));
			}	
		}
		//System.out.println(dateFormat.format( datum.getTime()));
		
		return dateFormat.format( datum.getTime());
	}
	
	
	public static String TimeLeft(String original)
	{
		/*
		 * 3 possible returns:
		 * 		1. ""			(time sent to function has passed)
		 * 		2. "0min"		(time sent to function is less that 1min)
		 * 		3. "?h ?min"	(time sent to function is greater or equal than 1min) 
		 * */
		
		String rezultat = "";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calOriginal = Calendar.getInstance();
		Calendar calCurrent = Calendar.getInstance();
		try {
			Date datum = (Date)dateFormat.parse(original);
			calOriginal.setTime(datum);
			//System.out.println(dateFormat.format(calOriginal.getTime()));
			//System.out.println(dateFormat.format(calCurrent.getTime()));
			
			long msRemaining = calOriginal.getTimeInMillis() - calCurrent.getTimeInMillis();
			if (msRemaining<=0){
				return "";
			} 
			
			
			//System.out.println(msRemaining);
			int intHours=0;
			int intMinutes=0;
			String strHours="";
			String strMinutes="";
			
			intHours = (int) (msRemaining / 3600000);
			msRemaining %= 3600000;
			intMinutes = (int) (msRemaining / 60000);
			msRemaining %= 60000;
			//System.out.println(intHours + "h"); 
			//System.out.println(intMinutes + "min");
			//System.out.println(msRemaining + "ms");
			
			if (msRemaining<=0){
				return "0min";
			} 
			
			if((intHours!=0)&&(intMinutes!=0)){
				rezultat=intHours + "h " + intMinutes + "min";
			}else{
				if ((intMinutes!=0)&&(intHours==0)){
					rezultat=intMinutes + "min";		
				}
				else if ((intMinutes==0)&&(intHours!=0)){
					rezultat=intHours + "h";
				}				
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		return rezultat;
	}
}
